home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / Chess / Source / Square.m < prev    next >
Text File  |  1994-04-01  |  3KB  |  156 lines

  1. #include <sys/types.h>
  2. #include <sys/time.h>
  3. #import <appkit/appkit.h>
  4.  
  5. #import "Square.h"
  6.  
  7. @implementation Square : Cell
  8. /*
  9.   This class represents one square on a chess board.  It has a color
  10.   and may contain a piece.
  11. */
  12.  
  13. - free
  14. {
  15.   if( icon )
  16.     free( icon );
  17.   [super free];
  18. }
  19.  
  20. - (int)type { return type; }
  21. - setIcon: (char const *)i
  22. {
  23.   if( icon )
  24.     free( icon );
  25.   if( !i ){
  26.     icon = 0;
  27.     return self;
  28.   }
  29.   icon = (char *)malloc( strlen(i) + 1 );
  30.   strcpy( icon, i );
  31. }
  32.  
  33. - (char const *)icon { return icon; }
  34.  
  35. - setBackground: (float) b { background = b; }
  36.  
  37. - drawSelf:(const NXRect *)f inView: v
  38. {
  39.   PSsetgray( background );
  40.   PSrectfill( f->origin.x, f->origin.y, f->size.width, f->size.height );
  41.   [self drawInside: f inView: v];
  42. }
  43. - drawBackground:(const NXRect *)f inView: v
  44. {
  45.   PSsetgray( background );
  46.   PSrectfill( f->origin.x, f->origin.y, f->size.width, f->size.height );
  47. }
  48.  
  49. - highlight: (const NXRect *)f inView: v
  50. {
  51.   struct timeval timeout;
  52.  
  53.   NXInsetRect( f, 1.0, 1.0 );
  54.   PSgsave();
  55.   PSsetlinewidth( 2.0 );
  56.   PSsetgray( NX_WHITE );
  57.   PSrectstroke ( f->origin.x, f->origin.y, f->size.width, f->size.height );
  58.   /***
  59.   PSmoveto( f->origin.x, f->origin.y );
  60.   PSrlineto( f->size.width, 0.0 );
  61.   PSrlineto( 0.0, f->size.height );
  62.   PSrlineto( -1.0 * f->size.width, 0.0 );
  63.   PSclosepath();
  64.   PSstroke();
  65.   ***/
  66.   // PSflushgraphics();
  67.   NXPing();
  68.   
  69.   timeout.tv_sec = 0;
  70.   timeout.tv_usec = 150000;
  71.   select( 0, 0, 0, 0, &timeout );
  72.  
  73.   PSsetgray( NX_BLACK );
  74.   PSrectstroke ( f->origin.x, f->origin.y, f->size.width, f->size.height );
  75.   /***
  76.   PSmoveto( f->origin.x, f->origin.y );
  77.   PSrlineto( f->size.width, 0.0 );
  78.   PSrlineto( 0.0, f->size.height );
  79.   PSrlineto( -1.0 * f->size.width, 0.0 );
  80.   PSclosepath();
  81.   PSstroke();
  82.   ***/
  83.   // PSflushgraphics();
  84.   NXPing();
  85.   
  86.   timeout.tv_sec = 0;
  87.   timeout.tv_usec = 150000;
  88.   select( 0, 0, 0, 0, &timeout );
  89.  
  90.   PSsetgray( NX_WHITE );
  91.   PSrectstroke ( f->origin.x, f->origin.y, f->size.width, f->size.height );
  92.   /***
  93.   PSmoveto( f->origin.x, f->origin.y );
  94.   PSrlineto( f->size.width, 0.0 );
  95.   PSrlineto( 0.0, f->size.height );
  96.   PSrlineto( -1.0 * f->size.width, 0.0 );
  97.   PSclosepath();
  98.   PSstroke();
  99.   ***/
  100.   // PSflushgraphics();
  101.   NXPing();
  102.   
  103.   timeout.tv_sec = 0;
  104.   timeout.tv_usec = 150000;
  105.   select( 0, 0, 0, 0, &timeout );
  106.  
  107.   PSsetgray( background );
  108.   PSrectstroke ( f->origin.x, f->origin.y, f->size.width, f->size.height );
  109.   /***
  110.   PSmoveto( f->origin.x, f->origin.y );
  111.   PSrlineto( f->size.width, 0.0 );
  112.   PSrlineto( 0.0, f->size.height );
  113.   PSrlineto( -1.0 * f->size.width, 0.0 );
  114.   PSclosepath();
  115.   PSstroke();
  116.   ***/
  117.   // PSflushgraphics();
  118.   NXPing();
  119.   
  120.   PSgrestore();
  121. }
  122.   
  123.  
  124.   
  125. - drawInside:(const NXRect *)r inView:v
  126. /*
  127.   Draw the chess piece.
  128. */
  129. {
  130.     NXPoint p;
  131.     NXSize s;
  132.     id bitmap;
  133.     char temp[1024];
  134.     
  135.     if( !icon )
  136.     return;
  137.     
  138.     /* Composite the piece icon in the center of the rect. */
  139.     bitmap = [NXImage findImageNamed: icon];
  140.     if (!bitmap){
  141.     [[NXBundle mainBundle] getPath: temp forResource: icon ofType: "tiff"];
  142.     bitmap = [[NXImage alloc] initFromFile: temp];
  143.     if (bitmap)
  144.         [bitmap setName: icon];
  145.     }
  146.     
  147.     [bitmap getSize: &s];
  148.     p.x = floor(((r->size.width - s.width) / 2.0) + r->origin.x);
  149.     p.y = floor(((r->size.height - s.height) / 2.0) + r->origin.y);
  150.     [bitmap composite: NX_SOVER toPoint: &p];
  151.     return self;
  152. }
  153.  
  154.  
  155. @end
  156.